package com.example.mychat.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.ImageView
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.mychat.R
import com.example.mychat.model.Message
import com.example.mychat.model.MessageStatus
import java.text.SimpleDateFormat
import java.util.*

class MessageAdapter(
    private val messages: List<Message>,
    private val onRetryClick: ((Message, Int) -> Unit)? = null
) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {

    companion object {
        private const val VIEW_TYPE_SENDER = 1
        private const val VIEW_TYPE_RECEIVER = 2
        private const val VIEW_TYPE_SYSTEM = 3
        private const val TIME_SPAN = 5 * 60 * 1000 // 5分钟的时间间隔
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
        val layout = when (viewType) {
            VIEW_TYPE_SENDER -> R.layout.item_message_sent
            VIEW_TYPE_SYSTEM -> R.layout.item_message_system
            else -> R.layout.item_message_received
        }
        val view = LayoutInflater.from(parent.context)
            .inflate(layout, parent, false)
        return MessageViewHolder(view)
    }

    override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
        val message = messages[position]
        holder.bind(message, position)
    }

    override fun getItemCount() = messages.size

    override fun getItemViewType(position: Int): Int {
        return when {
            messages[position].status == MessageStatus.SYSTEM -> VIEW_TYPE_SYSTEM
            messages[position].isSender -> VIEW_TYPE_SENDER
            else -> VIEW_TYPE_RECEIVER
        }
    }

    inner class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val messageText: TextView = itemView.findViewById(R.id.messageText)
        private val statusIcon: ImageView? = itemView.findViewById(R.id.statusIcon)
        private val retryButton: ImageView? = itemView.findViewById(R.id.retryButton)
        private val avatarImage: ImageView? = itemView.findViewById(R.id.avatarImage)
        private val usernameText: TextView? = itemView.findViewById(R.id.usernameText)

        fun bind(message: Message, position: Int) {
            messageText.text = message.content

            if (message.status == MessageStatus.SYSTEM) {
                messageText.setTextColor(ContextCompat.getColor(itemView.context, android.R.color.darker_gray))
                messageText.textAlignment = View.TEXT_ALIGNMENT_CENTER
                return
            }

            usernameText?.text = message.username

            val avatarResId = when(message.username) {
                "阿伟" -> R.drawable.avatar1
                "大橙子" -> R.drawable.avatar2
                "test3" -> R.drawable.avatar3
                "alice" -> R.drawable.avatar4
                "bob" -> R.drawable.avatar5
                "admin" -> R.drawable.avatar6
                else -> R.drawable.avatar1
            }
            avatarImage?.setImageResource(avatarResId)

            if (message.isSender) {
                when (message.status) {
                    MessageStatus.SENDING -> {
                        statusIcon?.visibility = View.VISIBLE
                        statusIcon?.setImageResource(R.drawable.ic_sending)
                        retryButton?.visibility = View.GONE
                    }
                    MessageStatus.SENT -> {
                        statusIcon?.visibility = View.GONE
                        retryButton?.visibility = View.GONE
                    }
                    MessageStatus.FAILED -> {
                        statusIcon?.visibility = View.GONE
                        retryButton?.visibility = View.VISIBLE
                        retryButton?.setOnClickListener {
                            onRetryClick?.invoke(message, position)
                        }
                    }
                    else -> {
                        statusIcon?.visibility = View.GONE
                        retryButton?.visibility = View.GONE
                    }
                }
            } else {
                statusIcon?.visibility = View.GONE
                retryButton?.visibility = View.GONE
            }
        }
    }
} 